package com.swu.pad.comment.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.swu.pad.activity.dao.ActivityDao;
import com.swu.pad.activity.entity.Activity;
import com.swu.pad.assignment.entity.AssignmentPad;
import com.swu.pad.assignment.service.AssignmentService;
import com.swu.pad.comment.dao.FbCommentDao;
import com.swu.pad.comment.entity.FbComment;
import com.swu.pad.comment.service.FbCommentService;
import com.swu.pad.course.entity.CourseGroupUser;
import com.swu.pad.util.EPLiteClient.EPLiteClient;

@Service
@Transactional
public class FbCommentServiceimpl implements FbCommentService {

	@Autowired
	private FbCommentDao fbcommentdao;
	@Autowired
	private ActivityDao activityDao;
	@Autowired
	private AssignmentService assignmentService;
	@Autowired
	private EPLiteClient epliteClient;

	/**
	 * 保存fb评论实体（组间评论）
	 */
	public FbComment saveComment(FbComment fbcomment) {

		return fbcommentdao.save(fbcomment);
	}

	public List<FbComment> FindCommentByAuthorId(String authorid, String activityId) {
		Activity activity = activityDao.findOne(activityId);
		CourseGroupUser courseGroupUser = assignmentService.getUserAssignmentSession(activity.getAssignmentId(), authorid);
		AssignmentPad assignmentPad = assignmentService.getUserAssignmentPad(activity.getAssignmentId(), courseGroupUser.getGroupId());
		return fbcommentdao.findFbCommentByAuthorIdAndPadId(authorid, assignmentPad.getId());
	}

	public List<FbComment> FindCommentByCommenerId(String commenerId, String activityId) {
		Activity activity = activityDao.findOne(activityId);
		CourseGroupUser courseGroupUser = assignmentService.getUserAssignmentSession(activity.getAssignmentId(), commenerId);
		AssignmentPad assignmentPad = assignmentService.getUserAssignmentPad(activity.getAssignmentId(), courseGroupUser.getGroupId());

		return fbcommentdao.findFbCommentByCommenterIdAndPadId(commenerId, assignmentPad.getId());
	}

	public List<FbComment> FindCommentByTypeAndUserId(String type, String userid, String activityId) {
		Activity activity = activityDao.findOne(activityId);
		CourseGroupUser courseGroupUser = assignmentService.getUserAssignmentSession(activity.getAssignmentId(), userid);
		AssignmentPad assignmentPad = assignmentService.getUserAssignmentPad(activity.getAssignmentId(), courseGroupUser.getGroupId());

		return fbcommentdao.findByFeedbackTypeAndAuthorIdAndPadId(type, userid, assignmentPad.getId());
	}

	public List<Map<String, Object>> getRelationship(String activityId, String userId) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Activity activity = activityDao.findOne(activityId);
		CourseGroupUser courseGroupUser = assignmentService.getUserAssignmentSession(activity.getAssignmentId(), userId);
		AssignmentPad assignmentPad = assignmentService.getUserAssignmentPad(activity.getAssignmentId(), courseGroupUser.getGroupId());
		List<FbComment> fbCommentList = fbcommentdao.findByPadId(assignmentPad.getId());
		Set<String> userSet = new HashSet<String>();
		for (int i = 0; i < fbCommentList.size(); i++) {
			userSet.add(fbCommentList.get(i).getAuthorId());
			userSet.add(fbCommentList.get(i).getCommenterId());
		}
		if (!userSet.isEmpty()) {
			String[] userArr = userSet.toArray(new String[userSet.size()]);
			for (int i = 0; i < userArr.length; i++) {
				for (int j = 0; j < userArr.length; j++) {
					if (userArr[i].equals(userArr[j]) || "".equals(userArr[i]) || null==userArr[i]|| "".equals(userArr[j]) || null==userArr[j])
						continue;
					Map<String, Object> tempMap = new HashMap<String, Object>();
					tempMap.put("author", epliteClient.getAuthorName(userArr[i]));
					tempMap.put("commenter", epliteClient.getAuthorName(userArr[j]));
					List<FbComment> tempFbCommentList1 = fbcommentdao.findByPadIdAndTypeAndAuthorIdAndCommenterId(assignmentPad.getId(), FbComment.TYPE_COMMENT, userArr[i],
							userArr[j]);
					tempMap.put("commentTimes", tempFbCommentList1.size());
					List<FbComment> tempFbCommentList2 = fbcommentdao.findByPadIdAndTypeAndAuthorIdAndCommenterId(assignmentPad.getId(), FbComment.TYPE_REPLY, userArr[i],
							userArr[j]);
					tempMap.put("replyTimes", tempFbCommentList2.size());
					List<FbComment> tempFbCommentList3 = fbcommentdao.findByPadIdAndTypeAndAuthorIdAndCommenterId(assignmentPad.getId(), FbComment.TYPE_FEEDBACK, userArr[i],
							userArr[j]);
					tempMap.put("feedbackTimes", tempFbCommentList3.size());
					list.add(tempMap);
				}
			}
		}
		return list;
	}

}
